<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="wechat" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- 判断数据内的children数组内.有没有数据来判断需不需要删除选项 -->
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 关闭esc关闭 关闭点击遮罩层点击关闭弹框 -->
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit?'编辑':'修改'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @closed="hClose"
      >
        <!-- 弹出框组件 -->
        <add-or-edit
          :id="curId"
          ref="depDialogRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @updateInfo="success"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>

</template>

<script>
import { getDepartments, deleteDepartments } from '@/api/departments'
import { tranListTotreeList } from '@/utils'
import addOrEdit from './deptDiglog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 公司树形图的数据
      list: [],
      showDialog: false,
      curId: '',
      // 判断为编辑还是新增. false为新增
      isEdit: false,
      originList: []
    }
  },
  created() {
    // 公司部门列表函数
    this.loadDepartments()
  },
  methods: {
    // 关闭表单触发的事件
    hClose() {
      // alert(1)
      // 调用子组件的重置表单事件 在diglog对话框关闭就会触发
      this.$refs.depDialogRef.resetForm()
    },
    // 子传父自定义事件
    success() {
      // 更新数据
      // 关闭窗口
      this.showDialog = false
      this.loadDepartments()
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.depDialogRef.loadDepartmentsDetail()
      })
    },
    // 添加部门 传递id
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确认
        try {
          await deleteDepartments(id)
          // 调用函数渲染页面
          this.loadDepartments()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        // 取消
      })
    },
    // 获取公司部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 需要传给子组件的四个对象 解构
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(res.data.depts, 'loadDepartments')
        res.data.depts.shift()
        console.log(res.data.depts, '取出第一个元素后的数组')
        this.list = tranListTotreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
